package com.ckh.springboot.service.impl;

import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.ckh.springboot.entity.Goods;
import com.ckh.springboot.entity.duihuanjiluVo;
import com.ckh.springboot.exception.ServiceException;
import com.ckh.springboot.mapper.GoodsMapper;
import com.ckh.springboot.service.IGoodsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author ckh
 * @since 2022-04-19
 */
@Service
public class GoodsServiceImpl extends ServiceImpl<GoodsMapper, Goods> implements IGoodsService {

    @Resource
    private GoodsMapper goodsMapper;

    @Override
    public Page<Goods> findPage(Page<Goods> page, String name) {
        return goodsMapper.findPage(page, name);
    }

    @Override
    public Page<duihuanjiluVo> getUserDhsp(Page<duihuanjiluVo> page,Integer userId) {
        return goodsMapper.getUserDhsp(page,userId);
    }

    @Transactional
    @Override
    public void setPersonGoods(Integer goodsId, Integer personId) {
        int useGood = goodsMapper.getUseGood(personId, goodsId);
        if(useGood > 0){
            throw new ServiceException("200","您已经选择过此商品了");
        }
        goodsMapper.deletePersonGoods(goodsId,personId );
        goodsMapper.setPersonGoods(goodsId,personId );
    }

    @Override
    public void deletePersonGoods(Integer goodsId, Integer personId) {
        goodsMapper.deletePersonGoods(goodsId,personId);
    }


}
